﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryManagement
{
    public partial class EditBookForm : Form
    {
        private Form parentForm;
        private int bookId;
        //private string adminId;
        public EditBookForm(Form parentForm)
        {
            InitializeComponent();
            this.parentForm = parentForm;
            //this.bookId = bookId;          
            //this.adminId = adminId;
            LoadBookDetails();
        }
        private void LoadBookDetails()
        {
            string query = $"SELECT ISBN, Title, Author, Category, Location, Stock FROM Books";
            DataTable dt = DatabaseHelper.ExecuteQuery(query);

            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                txtTitle.Text = row["Title"].ToString();
                txtAuthor.Text = row["Author"].ToString();
                txtISBN.Text = row["ISBN"].ToString();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string isbn = txtISBN.Text;
            string title = txtTitle.Text;
            string author = txtAuthor.Text;
            string category = cmbCategory.SelectedItem.ToString();
            string location = txtLocation.Text;
            int stock = int.Parse(txtStock.Text);

            string updateQuery = $"UPDATE Books SET Title = '{title}', Author = '{author}', Category = '{category}',Location='{location}',Stock='{stock}',ISBN='{isbn}' WHERE Bookid = '{isbn}'";
            DatabaseHelper.ExecuteNonQuery(updateQuery);

            // 记录日志
            string logQuery = $"INSERT INTO Logs (Action, BookID, Operator, Date) VALUES ('EditBook', {isbn}, '1', '{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}')";
            DatabaseHelper.ExecuteNonQuery(logQuery);

            MessageBox.Show("图书信息修改成功！");
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            parentForm.Show();
            this.Close();
        }
    }
}
